package com.cuz.hot100;

public class Q14longestCommonPrefix {
    public static void main(String[] args) {
//        System.out.println(longestCommonPrefix1(new String[]{"flower", "flow", "flowight"}));
        System.out.println(longestCommonPrefix1(new String[]{"aaa","aaa","aa"}));
    }

    public static String longestCommonPrefix1(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        StringBuilder res = new StringBuilder();
        boolean flag = true;
        int curLen = 0;
        while (flag) {
            if (curLen >= strs[0].length()) {
                break;
            }
            for (String str : strs) {
                flag = isCharAt(str, curLen, strs[0].charAt(curLen));
                if (!flag){
                    break;
                }
            }
            if (flag) {
                res.append(strs[0].charAt(curLen));
                curLen++;
            }
        }
        return res.toString();
    }

    private static boolean isCharAt(String str, int index, char c) {
        if (index >= str.length()) {
            return false;
        }
        return c == str.charAt(index);
    }
}
